angular.module('usercenter.module.userbelong', [])

    .config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
        $stateProvider
            .state('userbelong', {
                url: '/userbelong/:parm',
                templateUrl: uc_templateprefix + 'myview/userbelong.html',
                controller: 'UserbelongController'
            })
        ;
    }])
//登录页面控制器
    .controller('UserbelongController', ['$scope', '$state', '$window', '$stateParams', 'UserbelongService','AuthService', function ($scope, $state, $window, $stateParams, UserbelongService,AuthService) {
        var parm =$stateParams.parm;
        dismiss();
        var map, geolocation;
        var prompt;
        var cpoint;
        var NearplaceSearch;
        $scope.positionList = [];
        //加载地图，调用浏览器定位服务
        map = new AMap.Map('newMap', {
            resizeEnable: true,
            zoom:15
        });
        map.plugin('AMap.Geolocation', function() {
            geolocation = new AMap.Geolocation({
                enableHighAccuracy: true,//是否使用高精度定位，默认:true
                timeout: 10000,          //超过10秒后停止定位，默认：无穷大
                buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
                zoomToAccuracy: true,      //定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
                buttonPosition:'RB',
                showCircle: false,
                showMarker:true,
            });
            map.addControl(geolocation);
            geolocation.getCurrentPosition();
            AMap.event.addListener(geolocation, 'complete', onComplete);//返回定位信息
            AMap.event.addListener(geolocation, 'error', onError);      //返回定位出错信息
        });
        map.on('touchend', function (e) {
            prompt = map.getCenter();
            $scope.positionList = [];
            $('.nearbyPois').scrollTop(0,0);
            locationAnimation();
            cpoint = [prompt.lng,prompt.lat];
            serch_neraby($scope.city,cpoint);
        });
        var isOk = true;
        $(".nearbyPois").scroll(function () {
            var scrollTop = $(this).scrollTop();
            var scrollHeight = $("#address_point").height();
            var windowHeight = $(this).height();
            if (scrollTop + windowHeight >= scrollHeight) {
                if(isOk){
                    NearplaceSearch.L.pageIndex++;
                    isOk = false;
                    nearBymove(cpoint);
                }
            }
        });
        //解析定位结果
        function onComplete(data) {
            $scope.city = data.addressComponent.city;
            cpoint = [data.position.lng,data.position.lat];
            serch_neraby($scope.city,cpoint);
        }
        //解析定位错误信息
        function onError(data) {
            alert("定位失败,请搜索您想要的位置");
        }
        $scope.mapSearch = function(){
            serchmap($scope.city,'',$scope.addressSearch);
        };
        $scope.setposition = function(adcode,location){
            $(".contain-item").fadeIn();
            $scope.sure_click = function(){
                var userbelong = {
                    "zipcode": adcode,
                    "userid": $storage.userId,
                    "longitude": location.lng,
                    "latitude":location.lat,
                };
                if(parm ==2){
                    UserbelongService.setbelong(userbelong,function(data){
                        input_alert("添加成功！");
                        setTimeout(function(){
                            window.history.back();
                        },2000);
                        $(".contain-item").fadeOut();
                    },function(data){
                        input_alert("添加失败,请重试！");
                        $(".contain-item").fadeOut();
                    })
                }
                else if(parm == 1){
                    UserbelongService.updateByid(userbelong,function(data){
                        input_alert("更新成功");
                        $(".contain-item").fadeOut();
                        setTimeout(function(){
                            window.history.back();
                        },2000);
                    },function(data){
                        input_alert("更新失败，请重试！");
                        $(".contain-item").fadeOut();
                    })
                }
            }

        }
        //定位小动画
        function locationAnimation() {
            $("#mapLocation").css({
                top: "-8px",
                transition: "top 250ms ease",
                WebkitTransition: "top 250ms ease",
            });
            setTimeout(function () {
                $("#mapLocation").css({
                    top: "0px",
                    transition: "top 200ms ease",
                    WebkitTransition: "top 200ms ease",
                });
            }, 200);
        };
        function serchmap(city,type,messsg){
            AMap.service(["AMap.PlaceSearch"], function() {
                var placeSearch = new AMap.PlaceSearch({ //构造地点查询类
                    pageSize: 10,
                    pageIndex: 1,
                    city: city, //城市
                    map: map,
                    type:type,
                    //panel: "address_pois"
                });
                //关键字查询
                placeSearch.search(messsg,function(status,result){
                    if(status == "no_data"){
                        $("#no_nearsearch").show();
                    }else if (status == 'complete'){
                        $("#no_nearsearch").hide();
                        $scope.$apply(function() {
                            $scope.positionList = result.poiList.pois;
                        });
                    }
                });
            });
        };
        function serch_neraby(city,cpoint){
            AMap.service(["AMap.PlaceSearch"], function() {
                NearplaceSearch = new AMap.PlaceSearch({ //构造地点查询类
                    pageSize: 10,
                    type: '汽车服务|汽车销售|汽车维修|摩托车服务|餐饮服务|购物服务|生活服务|体育休闲服务|医疗保健服务|住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|交通设施服务|金融保险服务|公司企业|道路附属设施|地名地址信息|公共设施',
                    pageIndex: 1,
                    city: city, //城市
                    //map: map,
                    showCover:false,
                    extensions:'all',
                });
                nearBymove(cpoint);
            });
        };
        function nearBymove(cpoint){
            NearplaceSearch.searchNearBy('', cpoint, 300, function(status, result) {
                isOk = true;
                $scope.$apply(function() {
                    if(status == "no_data"){
                        $scope.isEnd = false;
                        $("#no_nearsearch").show();
                    }else if (status == 'complete'){
                        $("#no_nearsearch").hide();
                        for(var i=0;i<result.poiList.pois.length;i++){
                            $scope.positionList.push(result.poiList.pois[i]);
                        }
                        if(result.poiList.pois.length<10){
                            $scope.isEnd = false;
                        }
                        else{
                            $scope.isEnd = true;
                        }
                    }
                });
            });
        }
    }])


